﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class Modules_Personal_TargetAssignView : System.Web.UI.Page
{
    IPerf_Personal_TargetTypeHandler ttHandler = new Perf_Personal_TargetTypeManage();
    IPerf_Personal_FirstAssignHandler firstAssignHandler = new Perf_Personal_FirstAssignManage();
    IPerf_Personal_ModTargetHandler modTargetHandler = new Perf_Personal_ModTargetManage();
    IPerf_Personal_ModTargetDtlHandler modTargetDtlHandler = new Perf_Personal_ModTargetDtlManage();
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_OrganizationHandler orgHandler = new Sys_OrganizationManage();
    ISys_PostHandler postHandler = new Sys_PostManage();
    IPerf_Personal_TargetOwnerHandler toHandler = new Perf_Personal_TargetOwnerManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private void dataBind()
    {
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                try
                {
                    Perf_Personal_ModTargetQuery modTargetQuery = modTargetHandler.GetPerf_Personal_ModTargetQuery(ID);
                    if (modTargetQuery != null)
                    {
                        //Perf_Personal_ModTargetDtlQuery[] modTargetDtlList = modTargetDtlHandler.GetPerf_Personal_ModTargetDtlQueryList(
                        //    " ModTarget_ID = @ModTarget_ID", new SqlParameter[] { new SqlParameter("@ModTarget_ID", modTargetQuery.ID) });
                        //StringBuilder targetIds = new StringBuilder();
                        //if (modTargetDtlList != null && modTargetDtlList.Length > 0)
                        //{
                        //    foreach (Perf_Personal_ModTargetDtlQuery dtlQuery in modTargetDtlList)
                        //    {
                        //        targetIds.Append(dtlQuery.Target_ID);
                        //        targetIds.Append(",");
                        //    }
                        //    targetIds = targetIds.Remove(targetIds.Length - 1, 1);
                        //}
                        //DataTable targetList = targetHandler.GetPerf_Personal_TargetList(
                        //    " ID in (" + targetIds.ToString() + ")", new SqlParameter[] {
                        //    }, " TargetType_ID asc, ID asc");
                        DataTable targetList = modTargetDtlHandler.GetPerf_Personal_ModTargetDtlList(
                            " ModTarget_ID = @ModTarget_ID", new SqlParameter[] { new SqlParameter("@ModTarget_ID", modTargetQuery.ID) });

                        Perf_Personal_TargetQuery[] oriList = targetHandler.GetPerf_Personal_TargetQueryList(" TargetOwner_ID = @TargetOwner_ID", new SqlParameter[]{ new SqlParameter("@TargetOwner_ID", modTargetQuery.TargetOwner_ID) });
                        if (oriList != null && oriList.Length > 0)
                        {
                            foreach(Perf_Personal_TargetQuery tQuery in oriList)
                            {
                                bool isDelete = true;
                                for (int i = 0; i < targetList.Rows.Count; i++)
                                {
                                    int tid = int.Parse(targetList.Rows[i]["Target_ID"].ToString());
                                    if (tQuery.ID == tid)
                                    {
                                        isDelete = false;
                                        break;
                                    }
                                }

                                if (isDelete)
                                {
                                    DataRow row = targetList.NewRow();
                                    row["Target_ID"] = tQuery.ID.ToString();
                                    row["Type"] = (int)Enums.PersonalModTargetType.Deleted;
                                    targetList.Rows.Add(row);
                                }
                            }
                        }

                        gvTargetList.DataSource = targetList;
                        gvTargetList.DataBind();

                        Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(modTargetQuery.TargetUser_ID);
                        if (userQuery != null)
                            lblUser.Text = userQuery.Name;

                        Sys_OrganizationQuery orgQuery = orgHandler.GetSys_OrganizationQuery(modTargetQuery.Org_ID);
                        if (orgQuery != null)
                            lblOrg.Text = orgQuery.Name;

                        Sys_PostQuery postQuery = postHandler.GetSys_PostQuery(modTargetQuery.Post_ID);
                        if (postQuery != null)
                            lblPost.Text = postQuery.Name;

                        Perf_Personal_TargetOwnerQuery toQuery = toHandler.GetPerf_Personal_TargetOwnerQuery(modTargetQuery.TargetOwner_ID);
                        if (toQuery != null)
                            lblTargetOwner.Text = toQuery.Name;
                    }
                }
                catch
                {
                    Jscript.Alert("操作失败!", this.Page);
                }
            }
        }
    }

    protected void gvTargetList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                //Perf_Personal_TargetTypeQuery ttQuery = ttHandler.GetPerf_Personal_TargetTypeQuery(int.Parse(e.Row.Cells[2].Text));
                //if (ttQuery != null)
                //{
                //    e.Row.Cells[2].Text = ttQuery.Name;
                //}

                //e.Row.Cells[4].Text = Enums.GetYesNoStatus((Enums.YesNoStatus)int.Parse(e.Row.Cells[4].Text));

                Perf_Personal_TargetQuery targetQuery = targetHandler.GetPerf_Personal_TargetQuery(int.Parse(e.Row.Cells[0].Text));
                if (targetQuery != null)
                {
                    e.Row.Cells[0].Text = targetQuery.Code;
                    e.Row.Cells[1].Text = targetQuery.Name;
                    e.Row.Cells[3].Text = targetQuery.Score.ToString();
                    e.Row.Cells[4].Text = Enums.GetYesNoStatus((Enums.YesNoStatus)targetQuery.IsRequired);

                    Perf_Personal_TargetTypeQuery ttQuery = ttHandler.GetPerf_Personal_TargetTypeQuery(targetQuery.TargetType_ID);
                    if (ttQuery != null)
                    {
                        e.Row.Cells[2].Text = ttQuery.Name;
                    }
                }

                e.Row.Cells[5].Text = Enums.GetPersonalModTargetType((Enums.PersonalModTargetType)int.Parse(e.Row.Cells[5].Text));
            }
        }
    }

}
